[[...path]].page.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import CountBadge from '~/components/Common/CountBadge';
  10. import PageListIcon from '~/components/Icons/PageListIcon';
  11. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  12. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import { Page } from '~/components/Page';
  14. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  15. import TableOfContents from '~/components/TableOfContents';
  16. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  17. import { CrowiRequest } from '~/interfaces/crowi-request';
  18. import { RendererConfig } from '~/interfaces/services/renderer';
  19. import { IShareLinkHasId } from '~/interfaces/share-link';
  20. import {
  21. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  23. } from '~/stores/context';
  24. import { useDescendantsPageListModal } from '~/stores/modal';
  25. import loggerFactory from '~/utils/logger';
  26. import {
  27. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig,
  28. } from '../utils/commons';
  29. const logger = loggerFactory('growi:next-page:share');
  30. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  31. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  32. type Props = CommonProps & {
  33. shareLink?: IShareLinkHasId,
  34. isExpired: boolean,
  35. currentUser: IUser,
  36. disableLinkSharing: boolean,
  37. isSearchServiceConfigured: boolean,
  38. isSearchServiceReachable: boolean,
  39. isSearchScopeChildrenAsDefault: boolean,
  40. rendererConfig: RendererConfig,
  41. };
  42. const SharedPage: NextPage<Props> = (props: Props) => {
  43. useIsSearchPage(false);
  44. useShareLinkId(props.shareLink?._id);
  45. useCurrentPageId(props.shareLink?.relatedPage._id);
  46. useCurrentUser(props.currentUser);
  47. useCurrentPathname(props.currentPathname);
  48. useRendererConfig(props.rendererConfig);
  49. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  50. useIsSearchServiceReachable(props.isSearchServiceReachable);
  51. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  52. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  53. const { t } = useTranslation();
  54. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  55. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  56. const shareLink = props.shareLink;
  57. return (
  58. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  59. <div className="h-100 d-flex flex-column justify-content-between">
  60. <header className="py-0 position-relative">
  61. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  62. </header>
  63. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  64. <div className="flex-grow-1">
  65. <div id="content-main" className="content-main">
  66. <div className="grw-container-convertible">
  67. { props.disableLinkSharing && (
  68. <div className="mt-4">
  69. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  70. </div>
  71. )}
  72. { (isNotFound && !props.disableLinkSharing) && (
  73. <div className="container-lg">
  74. <h2 className="text-muted mt-4">
  75. <i className="icon-ban" aria-hidden="true" />
  76. <span> Page is not found</span>
  77. </h2>
  78. </div>
  79. )}
  80. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  81. <div className="container-lg">
  82. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  83. <h2 className="text-muted mt-4">
  84. <i className="icon-ban" aria-hidden="true" />
  85. <span> Page is expired</span>
  86. </h2>
  87. </div>
  88. )}
  89. {(isShowSharedPage && shareLink != null) && (
  90. <>
  91. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  92. <div className="d-flex flex-column flex-lg-row-reverse">
  93. <div className="grw-side-contents-container">
  94. <div className="grw-side-contents-sticky-container">
  95. {/* Page list */}
  96. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  97. { shareLink.relatedPage.path != null && (
  98. <button
  99. type="button"
  100. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  101. rounded-pill d-flex justify-content-between align-items-center"
  102. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  103. data-testid="pageListButton"
  104. >
  105. <div className="grw-page-accessories-control-icon">
  106. <PageListIcon />
  107. </div>
  108. {t('page_list')}
  109. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  110. </button>
  111. ) }
  112. </div>
  113. <div className="d-none d-lg-block">
  114. <TableOfContents />
  115. </div>
  116. </div>
  117. </div>
  118. <div className="flex-grow-1 flex-basis-0 mw-0">
  119. <Page />
  120. </div>
  121. </div>
  122. </>
  123. )}
  124. </div>
  125. </div>
  126. </div>
  127. </div>
  128. </ShareLinkLayout>
  129. );
  130. };
  131. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  132. const req: CrowiRequest = context.req as CrowiRequest;
  133. const { crowi } = req;
  134. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  135. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  136. props.isSearchServiceReachable = crowi.searchService.isReachable;
  137. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  138. props.rendererConfig = {
  139. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  140. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  141. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  142. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  143. plantumlUri: process.env.PLANTUML_URI ?? null,
  144. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  145. // XSS Options
  146. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  147. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  148. tagWhiteList: crowi.xssService.getTagWhiteList(),
  149. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  150. };
  151. }
  152. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  153. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  154. props._nextI18Next = nextI18NextConfig._nextI18Next;
  155. }
  156. function getAction(props: Props): SupportedActionType {
  157. let action: SupportedActionType;
  158. if (props.isExpired) {
  159. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  160. }
  161. else if (props.shareLink == null) {
  162. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  163. }
  164. else {
  165. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  166. }
  167. return action;
  168. }
  169. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  170. const req: CrowiRequest = context.req as CrowiRequest;
  171. const parameters = {
  172. ip: req.ip,
  173. endpoint: req.originalUrl,
  174. action,
  175. user: req.user?._id,
  176. snapshot: {
  177. username: req.user?.username,
  178. },
  179. };
  180. await req.crowi.activityService.createActivity(parameters);
  181. }
  182. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  183. const req = context.req as CrowiRequest<IUserHasId & any>;
  184. const { user, crowi, params } = req;
  185. const result = await getServerSideCommonProps(context);
  186. if (!('props' in result)) {
  187. throw new Error('invalid getSSP result');
  188. }
  189. const props: Props = result.props as Props;
  190. if (user != null) {
  191. props.currentUser = user.toObject();
  192. }
  193. try {
  194. const ShareLinkModel = crowi.model('ShareLink');
  195. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  196. if (shareLink != null) {
  197. props.isExpired = shareLink.isExpired();
  198. props.shareLink = shareLink.toObject();
  199. }
  200. }
  201. catch (err) {
  202. logger.error(err);
  203. }
  204. injectServerConfigurations(context, props);
  205. await injectNextI18NextConfigurations(context, props);
  206. await addActivity(context, getAction(props));
  207. return {
  208. props,
  209. };
  210. };
  211. export default SharedPage;